<?php

class DirectorsController extends AppController {

    var $name = 'Directors';
    var $paginate = array('limit' => 25);

    function admin_export($keyword=null) {
        $this->Direcror->recursive = -1;
        // Find fields needed without recursing through associated models
        $directors = $this->Director->find('all');

        $this->layout = false;
        $this->autoLayout = false;
        //pr($directors);
        $this->set('directors', $directors);
    }

    function admin_upload_csv() {
        if (!empty($this->data)) {
            //pr($this->data);
            if (!empty($this->data['Director']['fileName']['name']) && is_uploaded_file($this->data['Director']['fileName']['tmp_name'])) {
                
                if ($this->data['Director']['fileName']['type'] == "text/comma-separated-values" OR $this->data['Director']['fileName']['type'] == "text/x-csv" OR $this->data['Director']['fileName']['type'] == "application/octet-stream" OR $this->data['Director']['fileName']['type'] == "application/vnd.ms-excel") {
                    //$fileData = fread(fopen(, "r"),$this->data['Codetail']['fileName']['size']);
                    $messages = $this->Director->import($this->data['Director']['fileName']['tmp_name']);
                    //debug($messages);
                    if (isset($messages['errors']) && count($messages['errors']) > 0) {
                        $msg = implode("<br>", $messages['errors']);
                    } else {
                        $msg = implode("<br>", $messages['messages']);
                    }
                    //echo $msg;
                    $this->Session->setFlash($msg);
                } else {
                    $this->Session->setFlash(__('Invalid file type. You must upload a CSV file.', true));
                }
            } else {
                $this->Session->setFlash(__('CSV File Required.', true));
            }
        }
    }

    //update codexec
    function admin_update_codexec() {
        $this->loadModel("Codexec");
        $this->loadModel("Codtran");

        $this->Director->recursive = -1;
        $dirs = $this->Director->find("all", array("limit" => "13000,13050"));
        foreach ($dirs as $dir) {
            $this->Codexec->updateAll(array('Codexec.director_id' => $dir['Director']['id']), array('Codexec.Name' => $dir['Director']['name']));
            $this->Codtran->updateAll(array('Codtran.director_id' => $dir['Director']['id']), array('Codtran.Director' => $dir['Director']['name']));
        }
    }

    //fetch all directo from codexecs and codtrans
    function admin_fetch_directors() {
        $this->loadModel("Codexec");
        $this->Codexec->recursive = -1;
        $codexecs = $this->Codexec->find("all", array("limit" => "2000", "group" => "Codexec.Name", "order" => "Codexec.Name ASC"));
        foreach ($codexecs as $codexec) {
            $this->Director->query('INSERT INTO directors (id, name) VALUES ("", "' . trim($codexec['Codexec']['Name']) . '") ON DUPLICATE KEY UPDATE name = "' . trim($codexec['Codexec']['Name']) . '"');
        }
        // $this->autoRender=false;
    }

    function admin_fetch_codtran_directors() {
        $this->loadModel("Codtran");
        $this->Codtran->recursive = -1;
        $codexecs = $this->Codtran->find("all", array("limit" => "2000", "group" => "Codtran.Director", "order" => "Codtran.Director ASC"));
        foreach ($codexecs as $codexec) {
            $this->Director->query('INSERT INTO directors (id, name) VALUES ("", "' . trim($codexec['Codtran']['Director']) . '") ON DUPLICATE KEY UPDATE name = "' . trim($codexec['Codtran']['Director']) . '"');
        }
        //   $this->autoRender=false;
    }

    function index() {
        $this->Director->recursive = 0;
        $this->set('directors', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid director', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('director', $this->Director->read(null, $id));
    }

    function add() {
        if (!empty($this->data)) {
            $this->Director->create();
            if ($this->Director->save($this->data)) {
                $this->Session->setFlash(__('The director has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The director could not be saved. Please, try again.', true));
            }
        }
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid director', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Director->save($this->data)) {
                $this->Session->setFlash(__('The director has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The director could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Director->read(null, $id);
        }
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for director', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Director->delete($id)) {
            $this->Session->setFlash(__('Director deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Director was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }
	function convert_url($title=null) {
		$patterns = array('/<(.*?)>/', '/\s+/', '/\//', '/[^a-z0-9\-\._]/', '/\+/', '/-$/', '/_$/', '/^_/', '/^-/', '/\.+$/');
		$replace = array('', '_', '_', '', '_', '', '', '', '', '');
		$string = str_replace(' - ', '-', strtolower($title));
		//str_replace(array("-"," "),"_",strtolower($codexecs['Director']['name']))
		return preg_replace($patterns, $replace, $string);
		$this->layout = false;
		$this->autoRender = false;
    }
    function admin_index() {
		$i=0;
        $this->Director->recursive = 0;
		$directors=$this->paginate();
		//print_r($directors);
		foreach ($directors as $director) {
			$directorss[$i]['Director']['id'] = $director['Director']['id'];
			$directorss[$i]['Director']['name'] = $director['Director']['name'];
			$directorss[$i]['Director']['url_title'] = $this->convert_url($director['Director']['name']);
			$i++;
		}
        $this->set('directors', $directorss);
    }

    function admin_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid director', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('director', $this->Director->read(null, $id));
    }

    function admin_add() {
        if (!empty($this->data)) {
            $this->Director->create();
            if ($this->Director->save($this->data)) {
                $this->Session->setFlash(__('The director has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The director could not be saved. Please, try again.', true));
            }
        }
    }

    function admin_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid director', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Director->save($this->data)) {
                $this->Session->setFlash(__('The director has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The director could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Director->read(null, $id);
        }
    }

    function admin_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for director', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Director->delete($id)) {
            $this->Session->setFlash(__('Director deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Director was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

}
